hs.window.animationDuration = 0

-- 🔧 工具函数：取四舍五入整数
local function round(x)
	return math.floor(x + 0.5)
end

-- 🔧 获取窗口、屏幕、bundleID
local function getWinInfo()
	local win = hs.window.focusedWindow()
	if not win then
		return
	end
	local app = win:application()
	return win, win:frame(), win:screen():frame(), app:bundleID(), win:screen()
end

-- 🔧 判断是不是 Firefox
local function isFirefox(bundleID)
	return bundleID == "org.mozilla.firefox"
end

-- 🔧 计算 2/3 宽度（Firefox 至少 1168）
local function twoThirdWidth(sc_fm, bundleID)
	local w = round(sc_fm.w * 2 / 3)
	if isFirefox(bundleID) then
		w = math.max(w, 1168)
	end
	return w
end

-- 🔧 近似比较（解决 macOS 12 小数精度问题）
local function approxEqual(f1, f2)
	return math.abs(f1.x - f2.x) < 2
		and math.abs(f1.y - f2.y) < 2
		and math.abs(f1.w - f2.w) < 2
		and math.abs(f1.h - f2.h) < 2
end

-- 🔧 通用切换函数（多个布局之间切换）
local function toggleWin(win, frames)
	local f = win:frame()
	for i, target in ipairs(frames) do
		if approxEqual(f, target) then
			win:setFrame(frames[(i % #frames) + 1])
			return
		end
	end
	win:setFrame(frames[1])
end

-- 🔧 判断是否大于 MacBook Pro 16 的内置屏幕尺寸
local function isBigScreen(screen)
	local mode = screen:currentMode()
	local sz = screen:frame()
	-- 逻辑分辨率 * scale = 物理分辨率
	local w = sz.w * mode.scale
	local h = sz.h * mode.scale
	return w > 3456 or h > 2234
end

-- 最大化
hs.hotkey.bind({ "ctrl", "alt" }, "u", function()
	local win, f, sc_fm = getWinInfo()
	f.x, f.y, f.w, f.h = sc_fm.x, sc_fm.y, sc_fm.w, sc_fm.h
	win:setFrame(f)
end)

-- 左侧 1/2 / 2/3
hs.hotkey.bind({ "ctrl", "alt" }, "h", function()
	local win, f, sc_fm, bundleID, screen = getWinInfo()
	local w23 = twoThirdWidth(sc_fm, bundleID)
	local frames
	if isBigScreen(screen) then
		-- 大屏优先 1/2 → 2/3
		frames = {
			hs.geometry.rect(sc_fm.x, sc_fm.y, round(sc_fm.w / 2), sc_fm.h),
			hs.geometry.rect(sc_fm.x, sc_fm.y, w23, sc_fm.h),
		}
	else
		-- 小屏优先 2/3 → 1/2
		frames = {
			hs.geometry.rect(sc_fm.x, sc_fm.y, w23, sc_fm.h),
			hs.geometry.rect(sc_fm.x, sc_fm.y, round(sc_fm.w / 2), sc_fm.h),
		}
	end
	toggleWin(win, frames)
end)

-- 中间 2/3 / 1/2
hs.hotkey.bind({ "ctrl", "alt" }, "m", function()
	local win, f, sc_fm, bundleID, screen = getWinInfo()
	local w23 = twoThirdWidth(sc_fm, bundleID)
	local frames
	if isBigScreen(screen) then
		frames = {
			hs.geometry.rect(sc_fm.x + round(sc_fm.w / 4), sc_fm.y, round(sc_fm.w / 2), sc_fm.h),
			hs.geometry.rect(sc_fm.x + round((sc_fm.w - w23) / 2), sc_fm.y, w23, sc_fm.h),
		}
	else
		frames = {
			hs.geometry.rect(sc_fm.x + round((sc_fm.w - w23) / 2), sc_fm.y, w23, sc_fm.h),
			hs.geometry.rect(sc_fm.x + round(sc_fm.w / 4), sc_fm.y, round(sc_fm.w / 2), sc_fm.h),
		}
	end
	toggleWin(win, frames)
end)

-- 右侧 1/2 / 2/3
hs.hotkey.bind({ "ctrl", "alt" }, "l", function()
	local win, f, sc_fm, bundleID, screen = getWinInfo()
	local w23 = twoThirdWidth(sc_fm, bundleID)
	local frames
	if isBigScreen(screen) then
		frames = {
			hs.geometry.rect(sc_fm.x + round(sc_fm.w / 2), sc_fm.y, round(sc_fm.w / 2), sc_fm.h),
			hs.geometry.rect(sc_fm.x + sc_fm.w - w23, sc_fm.y, w23, sc_fm.h),
		}
	else
		frames = {
			hs.geometry.rect(sc_fm.x + sc_fm.w - w23, sc_fm.y, w23, sc_fm.h),
			hs.geometry.rect(sc_fm.x + round(sc_fm.w / 2), sc_fm.y, round(sc_fm.w / 2), sc_fm.h),
		}
	end
	toggleWin(win, frames)
end)

-- 上半屏
hs.hotkey.bind({ "ctrl", "alt" }, "k", function()
	local win, f, sc_fm = getWinInfo()
	f.x, f.y, f.w, f.h = sc_fm.x, sc_fm.y, sc_fm.w, round(sc_fm.h / 2)
	win:setFrame(f)
end)

-- 下半屏
hs.hotkey.bind({ "ctrl", "alt" }, "j", function()
	local win, f, sc_fm = getWinInfo()
	f.x, f.y, f.w, f.h = sc_fm.x, sc_fm.y + round(sc_fm.h / 2), sc_fm.w, round(sc_fm.h / 2)
	win:setFrame(f)
end)

-- 缩小窗口（水平 -100）
hs.hotkey.bind({ "ctrl", "alt" }, "-", function()
	local win, f, sc_fm = getWinInfo()
	f.w = math.max(400, f.w - 100) -- 保证最小 400
	if f.x + f.w > sc_fm.x + sc_fm.w then
		f.x = sc_fm.x + sc_fm.w - f.w -- 保持在屏幕内
	end
	win:setFrame(f)
end)

-- 放大窗口（水平 +100）
hs.hotkey.bind({ "ctrl", "alt" }, "=", function()
	local win, f, sc_fm = getWinInfo()
	f.w = math.min(sc_fm.w, f.w + 100)
	win:setFrame(f)
end)

-- 显示信息（调试用）
hs.hotkey.bind({ "ctrl", "alt" }, "i", function()
	local win, f, sc_fm, _, screen = getWinInfo()
	hs.alert.show(
		"Screen size: "
			.. sc_fm.w
			.. "x"
			.. sc_fm.h
			.. (isBigScreen(screen) and " → 大屏 (优先1/2)" or " → 小屏 (优先2/3)")
	)
end)
